class Solution {
    public int findKthLargest(int[] nums, int k) {
        //创建一个k个长度的小顶堆
        PriorityQueue<Integer> pq = new PriorityQueue<>(k);

        for(int i = 0; i < nums.length; i++) {
            if(i < k) { //先把前k元素放进pq
                pq.add(nums[i]);
            }else if(pq.peek() < nums[i]) {
                pq.poll();
                pq.add(nums[i]);
            }
        }

        return pq.peek();
    }
}